package com.shijie.controller.erp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.TalentTrials;
import com.shijie.service.TalentTrialsService;

import freemarker.template.utility.StringUtil;

/*
 *  @author: smart boy
 *  @date: 2019-01-15
 */
@RestController
@RequestMapping("/talenttrials")
public class TalentTrialsController {

    @Resource
	private TalentTrialsService talentTrialsService;

	@GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size,@RequestParam(required=false) Integer talentId,@RequestParam(required=false) String  q) {
        PageHelper.startPage(page, size);
        Condition condition = new Condition(TalentTrials.class);
        Example.Criteria c = condition.createCriteria();
        if(talentId != null){
        	c.andEqualTo("talentId", talentId);
        }
        if(!StringUtils.isEmpty(q)){
        	c.andLike("title", "%"+q+"%").orLike("subTitle", "%"+q+"%");
        }
        List<TalentTrials> list = talentTrialsService.selectByCondition(condition);
        PageInfo<TalentTrials> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
	}

	@PostMapping
    public Result add(@RequestBody TalentTrials talentTrials) {
        talentTrialsService.save(talentTrials);
        return ResultGenerator.success();
	}

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        talentTrialsService.deleteById(id);
        return ResultGenerator.success();
	}

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        TalentTrials talentTrials = talentTrialsService.selectById(id);
        return ResultGenerator.success(talentTrials);
	}
}

